#!/bin/sh
#
# $Id: postinst 60 2011-12-08 21:29:22Z imooreyahoo@gmail.com $
#
# see: dh_installdeb(1)

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
    configure)

	# Check for XML config
	if ! omv_config_exists "//services/cups"; then
		omv_config_add_element "//services" "cups" ""
	fi
	
    if ! omv_config_exists "//services/cups/enable"; then
    
        omv_config_add_element "//services/cups" "enable" "0"
        
        $(/etc/init.d/cups status >/dev/null && omv_config_update "//services/cups/enable" "1") || /bin/true
        
    fi

    if ! omv_config_exists "//services/cups/airprint"; then
    	omv_config_add_element "//services/cups" "airprint" "1"
    fi

    if ! omv_config_exists "//services/cups/enable_samba"; then
    	omv_config_add_element "//services/cups" "enable_samba" "1"
    fi

    if ! omv_config_exists "//services/cups/maxjobs"; then
    	omv_config_add_element "//services/cups" "maxjobs" "100"
    fi
	
	# Fix permissions
	omv_install_fixperms
	
	# Add lpadmin group
	groupadd -f lpadmin
	
    # Add lpadmin group to OMV
    if ! omv_config_exists "//system/usermanagement/groups/group[name='lpadmin']"; then    
        omv_config_import_group "lpadmin"
    fi
    
    omv_config_update "//system/usermanagement/groups/group[name='lpadmin']/comment" "Printer Administrators"
    
    # Silently add OMV admin to lpadmin group
    usermod -G lpadmin -a admin

	
	# For samba printer driver share
	chgrp -R lpadmin /var/lib/samba/printers
	chmod -R g+w /var/lib/samba/printers
	
	# Make sure python scripts are setup correctly
	chown root /usr/local/bin/omvcups.py
	chmod 700 /usr/local/bin/omvcups.py
	
	chown root /usr/local/bin/omvcups-avahi.py
	chmod 700 /usr/local/bin/omvcups-avahi.py
	
	
	# Check for en_US.UTF-8 locale
	if [ "$(grep -c '^\s*en_US.UTF-8' /etc/locale.gen)" = "0" ]; then
	
		# Just add it?
		if [ "$(grep -c '^\s*#\s*en_US.UTF-8' /etc/locale.gen)" = "0" ]; then
		
			echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen
		
		# Uncomment it
		else
		
			cat /etc/locale.gen | sed -e 's/^\s*#\s*\(en_US.UTF-8.*\)/\1/g' >/tmp/locale-gen.$$
			
			mv /tmp/locale-gen.$$ /etc/locale.gen
			
		fi
	
		locale-gen	
	fi
	

	
	
	# Perform actions if we're enabled
	if [ "$(omv_config_get "//services/cups/enable")" = "1" ]; then
	
		cupsctl --no-remote-printers

		# Generate cups config
		omv-mkconf cups
		
		# Restart cups
		/etc/init.d/cups restart
			
		# Avahi files
		/usr/local/bin/omvcups-avahi.py
		
		# Check for diff of samba config
		HASH="$(md5sum /etc/samba/smb.conf)"
		
		# Generate samba confing
		omv-mkconf samba
	
		if [ "$(omv_config_get "//services/smb/enable")" = "1" ]; then
		
			if [ "$(md5sum /etc/samba/smb.conf)" != "${HASH}" ]; then
				/etc/init.d/samba restart
			fi
			
		fi

		
	fi
		
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
